<template>
  <div class="app-container">
    <el-form :inline="true" :model="queryParam" class="demo-form-inline">
      <el-form-item>
        <el-input
          v-model="queryParam.id"
          placeholder="请输入用户编号"
          @input="searchQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="queryParam.username"
          placeholder="请输入用户名称"
          @input="searchQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="queryParam.phone"
          placeholder="请输入手机号"
          @input="searchQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          @click="searchQuery"
        >查询用户
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-refresh-right"
          @click="reset"
        >重置
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="handleAdd"
        >新增用户
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-upload
          ref="upload"
          :action="url.importExcelUrl"
          :limit="1"
          :show-file-list="false"
          :on-success="handleImportExcel"
          accept=".xls,.xlsx"
        >
          <el-button type="primary" icon="el-icon-download">导入用户</el-button>
        </el-upload>
      </el-form-item>
      <el-form-item v-if="$store.getters.role!=='admin'">
        <el-button
          type="primary"
          icon="el-icon-upload2"
          @click="handleExportExcel"
        >导出用户
        </el-button>
      </el-form-item>
      <el-form-item v-else>
        <el-button
          type="primary"
          icon="el-icon-s-promotion"
          plain
          @click="handleApply"
        >导出申请
        </el-button>
      </el-form-item>
      <el-form-item v-if="selectedIds.length > 0">
        <el-button
          type="danger"
          icon="el-icon-delete"
          @click="handleDeleteBatch()"
        >批量删除
        </el-button>
      </el-form-item>
    </el-form>
    <el-table
      ref="table"
      v-loading="loading"
      :data="tableData"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      @selection-change="handleSelectionChange"
      @select="handleSelect"
      @select-all="handleSelectAll"
    >
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column align="center" label="用户编号" width="200">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="用户名称" width="150" align="center">
        <template slot-scope="scope">
          {{ scope.row.username }}
        </template>
      </el-table-column>
      <el-table-column label="用户头像" width="150" align="center">
        <div slot-scope="scope" class="demo-basic--circle">
          <el-avatar size="small" :src="scope.row.avatar"/>
        </div>
      </el-table-column>
      <el-table-column label="真实姓名" width="150" align="center">
        <template slot-scope="scope">
          {{ scope.row.realName }}
        </template>
      </el-table-column>
      <el-table-column label="出生日期" width="150" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.birthday }}</span>
        </template>
      </el-table-column>
      <el-table-column label="性别" width="110" align="center">
        <template slot-scope="scope">
          {{ sexMap.get(scope.row.sex) }}
        </template>
      </el-table-column>
      <el-table-column label="电子邮箱" width="200" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.email }}</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号" width="150" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.phone }}</span>
        </template>
      </el-table-column>
      <el-table-column label="账号状态" width="110" align="center">
        <template slot-scope="scope">
          {{ statusMap.get(scope.row.status) }}
        </template>
      </el-table-column>
      <el-table-column label="创建日期" width="200" align="center">
        <template slot-scope="scope">
          {{ scope.row.gmtCreate }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="280" align="center">
        <template slot-scope="scope">
          <el-button
            type="primary"
            round
            size="small"
            @click.native.prevent="handleEdit(scope.$index, scope.row)"
          >
            编辑
          </el-button>
          <el-popconfirm
            confirm-button-text="确认"
            cancel-button-text="取消"
            icon="el-icon-info"
            icon-color="red"
            title="确定删除吗？"
            @onConfirm="handleDelete(scope.row.id)"
          >
            <el-button
              slot="reference"
              type="danger"
              round
              size="small"
              :disabled="$store.getters.role===scope.row.role"
            >
              删除
            </el-button>
          </el-popconfirm>
          <el-button
            type="success"
            round
            size="small"
            @click.native.prevent="handleDrawer(scope.row.id)"
          >
            角色分配
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block" style="text-align: right">
      <el-pagination
        :current-page="currentPage"
        :page-size="10"
        layout="total, prev, pager, next, jumper"
        :total="pageTotal"
        @current-change="handleCurrentChange"
      />
    </div>
    <UserModel ref="modalForm"/>
    <UserDrawer ref="drawer" :roles="roles"/>
  </div>
</template>

<script>
import UserModel from '@/views/ams/system/module/UserModel'
import {RCListMixin} from '@/mixins/RCListMixin'
import UserDrawer from '@/views/ams/system/UserDrawer'
import role from '@/api/system/role'
import store from '@/store'
import formApi from '@/api/common/formApi'

export default {
  components: {UserModel, UserDrawer},
  mixins: [RCListMixin],
  data() {
    return {
      formInfo: {
        usersId: '',
        applicantId: store.getters.id,
        status: 1
      },
      queryParam: {
        role: store.getters.role
      },
      roles: [],
      url: {
        list: '/system/sys-user/list',
        delete: '/system/sys-user/deleteById',
        deleteBatch: '/system/sys-user/deleteBatch',
        importExcelUrl: `${process.env.VUE_APP_BASE_API}/system/sys-user/importExcel`,
        exportExcelUrl: `/system/sys-user/exportExcel`
      },
      sexMap: new Map().set(1, '男').set(2, '女'),
      statusMap: new Map().set(1, '正常').set(2, '冻结'),
      excelName: '用户列表'
    }
  },
  created() {
    this.getAllRole()
  },
  methods: {
    onSubmit() {
      console.log('submit!')
    },
    handleDrawer(userId) {
      console.log('handleDrawer')
      console.log(userId)
      this.userId = userId
      this.$refs.drawer.visible = true
      this.$refs.drawer.userId = userId
      this.$refs.drawer.selectedIds = []
      this.$refs.drawer.getTableData()
      this.$refs.drawer.getUserRoleByUserId()
    },
    getAllRole() {
      role.getAllRole().then((res) => {
        console.log(res)
        this.roles = res.result
      })
    },
    send() {
      this.$router.push({
        path: `/resetPassword/123123`
      })
    },
    handleApply() {
      // console.log(this.selectedIds.join(','))
      this.formInfo.usersId = this.selectedIds.join(',')
      this.loading = true
      formApi.formRequest('/system/export-approve/add', 'Post', this.formInfo).then(
        (res) => {
          console.log(res)
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          this.loading = false
        },
        (err) => {
          console.log(err)
          this.$message.error('操作失败')
          this.loading = false
        }
      )
    }
  }
}
</script>
